/*
 * @Description:
 * @Version: 2.0
 * @Autor: yangzg
 * @Date: 2022-08-26 11:09:32
 * @LastEditors: wangmiao
 * @LastEditTime: 2022-09-16 16:37:03
 */
import axios from 'axios'
import { TEST_VARIABLE, EVN_VARIABLE } from '@/constants/env'


axios.defaults.timeout = 100000
axios.defaults.baseURL = import.meta.env.VITE_APP_ENV === EVN_VARIABLE ? '/api' : ''

const token =
  'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MTgsInVzZXJuYW1lIjoiYWRtaW4iLCJyb2xlIjoxLCJpYXQiOjE2NDIxMzk5NTAsImV4cCI6MTY0MjE1NDM1MH0.1SvXfv05JI5bd8-vND0_l6dptKBXdXK6X7IyZ3QCY08'

/**
 * http request 拦截器
 */

axios.interceptors.request.use(
  (config: any) => {
    config.data = JSON.stringify(config.data)
    config.headers = {
      'Content-Type': 'application/json',
      // Authorization: 'Bearer ' + token,
    }
    return config
  },
  (error: any) => {
    return Promise.reject(error)
  }
)

/**
 * http response 拦截器
 */
axios.interceptors.response.use(
  (response: any) => {
    if (response.data.errCode === 2) {
      console.log('过期')
    }
    return response
  },
  (error: any) => {
    console.log('请求出错：', error)
  }
)

/**
 * 封装get方法
 * @param url  请求url
 * @param params  请求参数
 * @returns {Promise}
 */

export function get(url: any, params?: Object): Promise<unknown> {
  return new Promise((resolve, reject) => {
    axios
      .get(url, {
        params: params,
      })
      .then((response: any) => {
        resolve(response)
      })
      .catch((error: any) => {
        reject(error)
      })
  })
}

/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function post(url: any, data?: Object): Promise<unknown> {
  return new Promise((resolve, reject) => {
    axios.post(url, data).then(
      (response: any) => {
        //关闭进度条
        resolve(response)
      },
      (err: any) => {
        reject(err)
      }
    )
  })
}
